package cn.itsource.hbc.org.controller;

import cn.itsource.hbc.org.domain.Department;
import cn.itsource.hbc.org.domain.Employee;
import cn.itsource.hbc.org.query.DepartmentQuery;
import cn.itsource.hbc.org.query.EmployeeQuery;
import cn.itsource.hbc.org.service.IDepartmentService;
import cn.itsource.hbc.org.service.IEmployeeService;
import cn.itsource.utils.AjaxResult;
import cn.itsource.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    public List<Employee> loadAll(){

        return employeeService.loadAll();
    }
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id){

       return employeeService.loadById(id);

    }

    @PutMapping
    public AjaxResult SaveOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId()!=null){
                employeeService.update(employee);
            }else {
                employeeService.save(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败"+e.getMessage());
        }

    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败"+e.getMessage());
        }

    }

    @PostMapping
    public PageResult<Employee> queryPage(@RequestBody EmployeeQuery query){

        return  employeeService.queryPage(query);
    }

    @PatchMapping
    public AjaxResult allDelete(@RequestBody Long[]ids){
        try {
            employeeService.allDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败"+e.getMessage());
        }


    }


}
